package com.hadwinling.service.impl;

import cn.hutool.core.io.file.FileReader;
import com.hadwinling.service.IoTDBService;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.write.record.Tablet;
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class IoTDBServiceImpl implements IoTDBService {
    @Autowired
    private Session iotdbSession;


    @Override
    public void setStorageGroup(String storageGroup) {
        try {
            iotdbSession.setStorageGroup(storageGroup);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteStorageGroup(String storageGroup) {
        try {
            iotdbSession.deleteStorageGroup(storageGroup);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insert(String storage, int zujianNum, int zhibiaoNum)  {
//        long start = System.currentTimeMillis();
        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        Random random = new Random();

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0 ;i <32; i++){
            measurements.add("s" + (i + 1));
            types.add(TSDataType.DOUBLE);
        }

        List<String> rhb_measurements = new ArrayList<>();
        List<TSDataType> rhb_types = new ArrayList<>();
        for (int i =0 ; i <98 ; i++){
            rhb_measurements.add("rhb"+(i+1));
            rhb_types.add(TSDataType.INT64);
        }

        List<MeasurementSchema> rhb_schemaList = new ArrayList<>();
        for (int i = 0 ; i< 98 ;i++){
            rhb_schemaList.add(new MeasurementSchema("s"+(i+1), TSDataType.INT64));
        }

        int line = 0;
        long timestamp = System.currentTimeMillis();
        for (int zj = 0; zj < 52; zj++) {
            for (int zb = 0; zb < 6; zb++) {
                String deviceId = storage + ".zj" + (zj + 1) + "." + shujukuaiArray[zb];
                List<Object> values = new ArrayList<>();

                for (int m = 0; m < 32; m++) {
                    double v = random.nextDouble();
                    values.add(v);
                }
                line++;
                deviceIds.add(deviceId);
                valuesList.add(values);
                measurementsList.add(measurements);
                typesList.add(types);
                timestamps.add(timestamp);
            }
        }
        try {
            iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
        } catch (IoTDBConnectionException e) {
            e.printStackTrace();
        } catch (StatementExecutionException e) {
            e.printStackTrace();
        }
        deviceIds.clear();
        measurementsList.clear();
        valuesList.clear();
        timestamps.clear();
//        System.out.println((System.currentTimeMillis()-start));
    }

    @Override
    public void insertWithRHB(String storage, int zujianNum, int zhibiaoNum) {
        Random random = new Random();
        String[] shujukuaiArray = {"gl", "kq", "rq", "s", "d", "wd"}; //gl:功率 kq：快群 rq：热群 s: 氙 d: 碘 wd：温度

        List<String> deviceIds = new ArrayList<>();
        List<List<String>> measurementsList = new ArrayList<>();
        List<List<Object>> valuesList = new ArrayList<>();
        List<Long> timestamps = new ArrayList<>();
        List<List<TSDataType>> typesList = new ArrayList<>();

        List<String> measurements = new ArrayList<>();
        List<TSDataType> types = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            measurements.add("s" + (i + 1));
            types.add(TSDataType.DOUBLE);
        }

        List<String> rhb_measurements = new ArrayList<>();
        List<TSDataType> rhb_types = new ArrayList<>();
        for (int i = 0; i < 98; i++) {
            rhb_measurements.add("rhb" + (i + 1));
            rhb_types.add(TSDataType.INT64);
        }

        List<MeasurementSchema> rhb_schemaList = new ArrayList<>();
        for (int i = 0; i < 98; i++) {
            rhb_schemaList.add(new MeasurementSchema("s" + (i + 1), TSDataType.INT64));
        }


        long timestamp = System.currentTimeMillis();
        for (int rhb = 0; rhb < 18; rhb++) {

            // 处理燃耗步开始
            Tablet tablet = new Tablet(storage + "rhb_" + (rhb + 1), rhb_schemaList, 98);
            // 处理燃耗步结束

            for (int sk = 0; sk < 98; sk++) {
                for (int zj = 0; zj < 52; zj++) {
                    for (int zb = 0; zb < 6; zb++) {
                        String deviceId = storage + ".zj" + (zj + 1) + "." + shujukuaiArray[zb];
                        List<Object> values = new ArrayList<>();
                        for (int m = 0; m < 32; m++) {
                            double v = random.nextDouble();
                            values.add(v);
                        }
                        deviceIds.add(deviceId);
                        valuesList.add(values);
                        measurementsList.add(measurements);
                        typesList.add(types);
                        timestamps.add(timestamp);
                    }
                }
                try {
                    iotdbSession.insertRecords(deviceIds, timestamps, measurementsList, typesList, valuesList);
                } catch (IoTDBConnectionException e) {
                    e.printStackTrace();
                } catch (StatementExecutionException e) {
                    e.printStackTrace();
                }
                deviceIds.clear();
                measurementsList.clear();
                valuesList.clear();
                timestamps.clear();

                // 处理燃耗步开始
                tablet.addValue(rhb_schemaList.get(sk).getMeasurementId(), sk, timestamp);
                // 处理燃耗步结束

                timestamp = timestamp + 500;
            }
            try {
                iotdbSession.insertTablet(tablet);
            } catch (StatementExecutionException e) {
                e.printStackTrace();
            } catch (IoTDBConnectionException e) {
                e.printStackTrace();
            }
            tablet.reset();
        }
    }

}
